define(function(require){
	var $=require("jq");
	var Dialog=require("pz_use_dialog.js");
	
	var account = $("#account");
	var transferMoney = $("#transferMoney");

$(function(){
	
	var time = $('#balanceSubmit').attr('time');
	if(time > 0){
		var s = 120 - Math.floor(time/(1000));
		var intervalId=setInterval(function(){
			if(s > 0){
				$(".timeOut").html("（"+s+"秒内无法再次提交）");
				$(".go-check").addClass("dis");
				$("#balanceSubmit").attr("dis","1");
				s-=1;
			}else{
				$(".timeOut").html('');
				$("#balanceSubmit").removeClass("dis");
				$("#balanceSubmit").attr("dis","0");
				clearInterval(intervalId);
			}
		},1000);
	}
	
	/**
	 * 校验收款人装好是否正确
	 */
	account.bind("change",function(){
		var post_data = {};
		post_data['u2LoginId'] = account.val();
		$.post("/EMoneyTransferUserChecker.do",post_data,function(data){
			var result = eval('('+data+')');
			if(result['ok'] == 1){
				errorTrip(account,'');
				return;
			}else{
				var error = result['error'];
				errorTrip(account,error);
			}
		});
	});
	/**
	 * 转账金额校验
	 */
	transferMoney.bind("change",function(){
		var post_data = {};
		post_data['amount'] = transferMoney.val();
		$.post("/EMoneyTransferAmountChecker.do",post_data,function(data){
			var result = eval('('+data+')');
			if(result['ok'] == 1){
				errorTrip(transferMoney,'');
				return;
			}else{
				var error = result['error'];
				errorTrip(transferMoney,error);
			}
		});
	});
	
	$("#balanceSubmit").bind("click",function(){
		if(account.val()==""){
			errorTrip(account,"收款人帐号不能为空");
			return
		}
		if(transferMoney.val()==""){
			errorTrip(transferMoney,"转账金额不能为空");
			return
		}
		if(transferMoney.val().indexOf('.') > 0){
			if(transferMoney.val().substring(transferMoney.val().indexOf('.')+1,transferMoney.val().length).length > 2){
				errorTrip(transferMoney,"金额请保留两位小数");
				return
			}
		}
		var errors = 0;
		$.each($('.inter .errorTrip'), function(i, n){
			if($(n).html().length > 0){
				errors+=1;
			}
		});
		if(errors == 0){
			if($(this).hasClass("dis")){
				return
			}
			transferMoneys();
		}
	});
	
	$(".cancel").bind("click",function(){
		$(".layer,.thickdiv").hide();
		location.reload();
	});
	
	function errorTrip(dom,text){
		dom.next().html(text);
	}
	
	function sucTrip(dom){
		dom.next().html("");
	}
	
	/**
	 * 给手机发送交易验证码然后弹出对话框
	 */
	function transferMoneys(){
		var sendDate = {};
		var amount = transferMoney.val();
		sendDate['amount'] = amount;
		$.get("/sendTransferVerify.do",sendDate,function(data){
			var result = eval('('+data+')');
			if(result['ok'] == 1){
				var phoneNumber = result['phoneNumber'];
				$('#phoneNumber').html(phoneNumber);
				$(".layer").show();
				center($(".layer"));
				timer();
			}else if(result['bind_mobile']==0){
				var tips=new Dialog({
					  type:"alert",
					  text:"系统提示",
					  tipsText:'&nbsp;&nbsp;&nbsp;&nbsp;您还未绑定手机请您先去绑定手机。',
					  buttonSureText:"去绑定手机", 
					  buttonCancelText:"关闭", 
					  callBack:function(){         //回调函数
						  window.location.href="/vipCenter/completeUserData.do?menu_id=3";
						  return;
					  },
					  cancelCallBack:function(){
					  },
					  mask:0.8,
					  isDrag:true                //开起拖动
				});
				return;
			}else{
				var tips=new Dialog({
					  type:"alert",
					  text:"系统提示",
					  tipsText:result['error'],
					  buttonSureText:"知道了", 
					  buttonCancelText:"关闭", 
					  callBack:function(){         //回调函数
					  },
					  cancelCallBack:function(){
					  },
					  mask:0.8,
					  isDrag:true                //开起拖动
				});
				return;
			}
		});
	}
	
	var center = function(dom){
	 	 var top = ($(window).height() - dom.height())/2;
         var left = ($(window).width() - dom.width())/2;
         var scrollTop = $(document).scrollTop();
         var scrollLeft = $(document).scrollLeft();

         if (top<0) {
          var top = 30;
         }
         var thickdiv = '<div class="thickdiv"></div>';
         $("body").append(thickdiv);
         $(".thickdiv").css({"height":$(window).height()});
         dom.css( { position : 'absolute', 'top' : top+scrollTop, left : left + scrollLeft } );
	}
	
//	倒计时发送验证码开始
	var time = 120;
	var t;
	var inputBtn = $("#timeout");
	function timer(){
		var t = window.setTimeout(function(){
				if(time==0){
					clearTimeout(t);
					inputBtn.val("重新获取验证码");
					inputBtn.attr("disabled",false);
					time= 120;
					return;
				}
				inputBtn.val(time+"秒后重新获取");
				inputBtn.attr("disabled",true);
				time--;
				timer();
		},1000);
	}
//	var newTime = 120;
//	var n;
//	function newTimer(){
//		//$(".timeOut").html("（120秒内无法再次提交）");
//		$(".go-check").addClass("dis");
//		var n = window.setTimeout(function(){
//				if(newTime==0){
//					clearTimeout(n);
//					$(".timeOut").html("");
//					$("#balanceSubmit").removeClass("dis");
//					$("#balanceSubmit").attr("dis","0");
//					newTime= 120;
//					return;
//				}
//				$(".timeOut").html("（"+newTime+"秒内无法再次提交）");
//				newTime--;
//				newTimer();
//		},1000);
//	}

	
	//重新获取
	$("#timeout").bind("click",function(){
		$.get("/sendTransferVerify.do",function(data){
			var result = eval('('+data+')');
			if(result['ok'] == 1){
				$(this).attr("disabled",true);
				timer();
			}else{
				error_content.html(result['error']);
				return;
			}
		});
	});
	
	var code_input = $('#code');
	var error_content = $('.content-input .errorTrip');
	$('#submitCode').bind('click',function(){
		if(code_input.val()==""){
			error_content.html('验证码不能为空');
			return;
		}
		var post_data = {};
		post_data['code'] = code_input.val();
		$.post("/verify.do",post_data,function(data){
			var result = eval('('+data+')');
			if(result['ok'] == 1){
				error_content.html('');
				var account_val = account.val();
				var transferMoney_val = transferMoney.val();
				var code = code_input.val();
				
				var obj = {};
				obj['u2LoginId'] = account_val;
				obj['amount'] = transferMoney_val;
				obj['code'] = code;
				$(".layer,.thickdiv").hide();
				$.post("/EMoneyTransfer.do",obj,function(data){
					var res = eval('('+data+')');
					if(res['ok']==1){
						var tips=new Dialog({
							  type:"alert",
							  text:"系统提示",
							  tipsText:'E钱包转帐成功',
							  buttonSureText:"知道了", 
							  buttonCancelText:"关闭", 
							  callBack:function(){         //回调函数
								    account.val('');
									transferMoney.val('');
									code_input.val('');
							  },
							  cancelCallBack:function(){
							  },
							  mask:0.8,
							  isDrag:true                //开起拖动
						});
					}else{
						var tips=new Dialog({
							  type:"alert",
							  text:"系统提示",
							  tipsText:res['error'],
							  buttonSureText:"知道了", 
							  buttonCancelText:"关闭", 
							  callBack:function(){         //回调函数
								    account.val('');
									transferMoney.val('');
									code_input.val('');
							  },
							  cancelCallBack:function(){
							  },
							  mask:0.8,
							  isDrag:true                //开起拖动
						});
					}
				});
			}else{
				error_content.html(result['error']);
				return;
			}
		});
	});
});
})